package com.shopping.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.common.utils.PageUtils;
import com.shopping.common.utils.Query;
import com.shopping.common.utils.R;
import com.shopping.ware.dao.WareSkuDao;
import com.shopping.ware.entity.PurchaseDetailEntity;
import com.shopping.ware.entity.WareSkuEntity;
import com.shopping.ware.feign.ProductFeignService;
import com.shopping.ware.service.WareSkuService;
import com.shopping.common.vo.SkuHasStockVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Resource
    WareSkuDao wareSkuDao;

    @Resource
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)) {
            wrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void addStock(PurchaseDetailEntity entity) {
        //1.如果没有库存记录新增
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id", entity.getSkuId());
        wrapper.eq("ware_id", entity.getWareId());
        List<WareSkuEntity> entities = wareSkuDao.selectList(wrapper);
        if (entities != null && entities.size() > 0) {
            wareSkuDao.addStock(entity);
        } else {
            WareSkuEntity skuEntity = new WareSkuEntity();
            skuEntity.setSkuId(entity.getSkuId());
            skuEntity.setStock(entity.getSkuNum());
            skuEntity.setWareId(entity.getWareId());
            skuEntity.setStockLocked(0);
            //若失败无需回滚
            try {
                R info = productFeignService.info(entity.getSkuId());
                @SuppressWarnings("unchecked")
                Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                if (info.getCode() == 0) {
                    skuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
                log.error("unchecked exception");
            }

            wareSkuDao.insert(skuEntity);
        }
    }

    @Override
    public List<SkuHasStockVo> getSkuStock(List<Long> skuIds) {
        return skuIds.stream().map(skuId -> {
            SkuHasStockVo vo = new SkuHasStockVo();
            Long count = baseMapper.getSkuStock(skuId);
            vo.setSkuId(skuId);
            vo.setHasStock(count != null && count > 0);
            return vo;
        }).collect(Collectors.toList());
    }

}
